﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace QLCN
{
    public partial class frmDmDoituongMT : Form
    {
        private SqlDataAdapter sqlDataAdapter;
        private SqlConnection sqlConnection;
        private SqlCommand sqlCommand;
        private SqlDataReader sqlDataReader;
        private DataSet sqlDataSet;
        private DataTable sqlTable;
        private String Maso;
        private string ServerName = Chung.ServerName;
        private string DatabaseName = Chung.DatabaseName;
        private string UserDatabase = Chung.UserDatabase;
        private string PassWordDatabase = Chung.PasswordDatabase;
        private string ChuoiKetNoi = "";
        private string chedo = "";

        public frmDmDoituongMT()
        {
            InitializeComponent();
        }

        private void frmDmDoituongMT_Load(object sender, EventArgs e)
        {
            ChuoiKetNoi = "Data Source = " + ServerName + ";" +
                         "Initial Catalog = " + DatabaseName + ";" +
                         "persist security info = true;" +
                         "User Id=" + UserDatabase +
                         "; Password='" + PassWordDatabase +
                         "'; Connect Timeout =50";
            //mo ket noi
            sqlConnection = new SqlConnection(ChuoiKetNoi);
            sqlConnection.Open();
            Display();
            Setcontrol(false);
        }

        private void frmDmDoituongMT_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyValue)
            {

                case 113:
                    luu();
                    break;

                case 27:
                    {
                        sqlConnection.Close();
                        this.Close();
                    }

                    break;

                case 114:
                    {
                        Moi();
                        break;
                    }

                case 115:
                    {
                        sua();
                        break;
                    }
                case 117:
                    {
                        xoa();
                        break;
                    }

                case 112:
                    {
                        //Tro giup
                        break;
                    }

            }
        }

        private void Setcontrol(bool T)
        {
            if (T)
            {
                txtMaDT.Enabled = true;
                txtTenDT.Enabled = true;
                btnThem.Enabled = false;
                btnLuu.Enabled = true;

            }
            else
            {
                txtMaDT.Enabled = false;
                txtTenDT.Enabled = false;
                btnThem.Enabled = true;
                btnLuu.Enabled = false;
            }
        }
        private void Display()
        {
            string dulieu = "", table = "";

            try
            {
                dulieu = " idDoiTuongMT, MaDoiTuongMT, TenDoiTuongMT";
                table = "DmDoiTuongMT";


                sqlCommand = new SqlCommand("spDislay", sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;

                //lấy giá trị vào biến    
                sqlCommand.Parameters.AddWithValue("@dulieu", dulieu.Trim());
                sqlCommand.Parameters.AddWithValue("@Table", table.Trim());

                //thực thi truy van 
                sqlCommand.ExecuteNonQuery();

                sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                sqlDataSet = new DataSet();
                sqlDataAdapter.Fill(sqlDataSet, "DmDoiTuongMT");

                sqlTable = sqlDataSet.Tables[0];
                //chuyen len luoi
                dataGridView1.DataSource = sqlTable;
                dataGridView1.AutoGenerateColumns = false;

                sqlDataAdapter.Dispose();
                sqlCommand.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Loi!");
            }


        }


        private void btnThem_Click(object sender, EventArgs e)
        {
            Moi();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            sua();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            luu();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            xoa();
        }

        private void BntLui_Click(object sender, EventArgs e)
        {
            sqlConnection.Close();
            this.Close();
        }

        private void Moi()
        {
            txtMaDT.Text = "";
            txtTenDT.Text = "";
            btnThem.Enabled = false;
            btnSua.Enabled = true;
            btnLuu.Enabled = true;

            Setcontrol(true);
            chedo = "them";
            txtMaDT.Focus();
        }

        private void sua()
        {
            Setcontrol(true);
            btnThem.Enabled = true;
            btnSua.Enabled = false;
            btnLuu.Enabled = true;
            chedo = "sua";

            btnLuu.Focus();
        }

        private void luu()
        {
            try
            {
                string table = "", dulieu = "", truongdl = "", dieukien = "";
                table = "DmDoiTuongMT";

                switch (chedo)
                {

                    case "them":
                        {


                            truongdl = " MaDoiTuongMT,TenDoiTuongMT ";

                            if ((txtMaDT.Text != "") && (txtTenDT.Text != ""))
                            {
                                dulieu = " '" + txtMaDT.Text.Trim() + "', N'" + txtTenDT.Text.Trim() + "' ";

                                sqlCommand = new SqlCommand("spNhapmoi", sqlConnection);
                                sqlCommand.CommandType = CommandType.StoredProcedure;
                                //lấy giá trị vào biến    
                                sqlCommand.Parameters.AddWithValue("@Table", table.Trim());
                                sqlCommand.Parameters.AddWithValue("@TruongDl", truongdl.Trim());
                                sqlCommand.Parameters.AddWithValue("@dulieu", dulieu.Trim());

                                //thực thi truy van 
                                sqlCommand.ExecuteNonQuery();
                                sqlCommand.Dispose();

                                Display();
                                Setcontrol(false);
                                //  btnThem.Enabled = true;
                                // btnSua.Enabled = true;
                                txtMaDT.Text = "";
                                txtTenDT.Text = "";
                                txtMaDT.Focus();
                            }
                            else
                            {
                                MessageBox.Show("Bạn nhập chưa đầy đủ dữ liệu", "Thông báo");
                                txtMaDT.Focus();
                            }
                            break;
                        }

                    case "sua":
                        {


                            if ((txtMaDT.Text != "") && (txtTenDT.Text != ""))
                            {
                                dulieu = " MaDoiTuongMT = '" + txtMaDT.Text.Trim() + "',TenDoiTuongMT = N'" + txtTenDT.Text.Trim() + "' ";

                            }


                            dieukien = "idDoiTuongMT = '" + Maso.Trim() + "'";


                            sqlCommand = new SqlCommand("spCapnhat", sqlConnection);
                            sqlCommand.CommandType = CommandType.StoredProcedure;

                            //lấy giá trị vào biến 
                            sqlCommand.Parameters.AddWithValue("@Table", table.Trim());
                            sqlCommand.Parameters.AddWithValue("@dulieu", dulieu.Trim());
                            sqlCommand.Parameters.AddWithValue("@Dieukien", dieukien.Trim());

                            
                            //thực thi truy van 
                            sqlCommand.ExecuteNonQuery();
                            sqlCommand.Dispose();

                            Display();
                            Setcontrol(false);

                            btnLuu.Enabled = true;
                            btnSua.Enabled = false;
                            txtMaDT.Text = "";
                            txtTenDT.Text = "";
                            txtMaDT.Focus();

                            break;
                        }

                }



            }

            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi cập nhật : " + ex.Message, "Thông báo :");
            }
        }

        private void xoa()
        {
            string table = "", dieukien = "";
            if (!string.IsNullOrEmpty(Maso))
            {
                if (MessageBox.Show("Bạn muốn xóa Đối Tượng : " + txtTenDT.Text.Trim() + " ? ", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    table = " DmDoiTuongMT";
                    dieukien = " idDoiTuongMT = '" + Maso.Trim() + "'";

                    sqlCommand = new SqlCommand("spXoa", sqlConnection);
                    sqlCommand.CommandType = CommandType.StoredProcedure;

                    //lấy giá trị vào biến                                
                    sqlCommand.Parameters.AddWithValue("@Table", table.Trim());
                    sqlCommand.Parameters.AddWithValue("@Dieukien", dieukien.Trim());

                    //thực thi truy van                                     
                    sqlCommand.ExecuteNonQuery();
                    sqlCommand.Dispose();

                    Display();
                    Setcontrol(false);
                    btnSua.Enabled = true;
                    btnThem.Enabled = true;
                    btnLuu.Enabled = false;
                    txtMaDT.Text = "";
                    txtTenDT.Text = "";
                    txtMaDT.Focus();
                }
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;

            Maso = dataGridView1.CurrentRow.Cells[0].Value.ToString();

            txtMaDT.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            txtTenDT.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();


            txtMaDT.Enabled = true;
            txtTenDT.Enabled = true;

        }

       
    }
}
